package pers.cj.pdos.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import pers.cj.pdos.cache.CacheData;
import pers.cj.pdos.cache.CacheRemove;
import pers.cj.pdos.cache.CacheRemoveMore;
import pers.cj.pdos.entity.Unit;
import pers.cj.pdos.mapper.UnitMapper;
import pers.cj.pdos.service.IUnitService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import pers.cj.pdos.utils.Utils;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chenj
 * @since 2019-11-04
 */
@Service
public class UnitServiceImpl extends ServiceImpl<UnitMapper, Unit> implements IUnitService {
    @Autowired
    UnitMapper unitMapper;
    @Override
    public List<Unit> listPage(Integer page, Integer limit, String companyId, String searchContent, Integer status) {
        QueryWrapper<Unit> wrapper = new QueryWrapper<>();
        wrapper.eq("company_id",companyId);
        if (status!=null){
            wrapper.eq("status",status);
        }

        if (!Utils.isEmpty(searchContent)) {
            wrapper.like("name",searchContent);
        }

        if (page !=null && limit != null) {
            IPage<Unit> unitIPage = unitMapper.selectPage(new Page<Unit>(page,limit), wrapper);
            return unitIPage.getRecords();
        } else {
            return unitMapper.selectList(wrapper);
        }
    }

    @Override
    public List<Unit> list(String companyId) {
        return unitMapper.selectList(new QueryWrapper<Unit>().eq("company_id", companyId));
    }

    @Override
    public List<Unit> listNormal(String companyId) {
        return unitMapper.selectList(new QueryWrapper<Unit>().eq("company_id", companyId).eq("status", 1));
    }

    @Override
    public long count(String companyId, String searchContent, Integer status) {
        QueryWrapper<Unit> wrapper = new QueryWrapper<>();
        wrapper.eq("company_id",companyId);
        if (status!=null){
            wrapper.eq("status",status);
        }

        if (!Utils.isEmpty(searchContent)) {
            wrapper.like("name",searchContent);
        }
        return unitMapper.selectCount(wrapper);
    }

    @Override
    public boolean checkName(String name, String companyId) {
        return unitMapper.selectCount(new QueryWrapper<Unit>().eq("company_id",companyId).eq("name",name)) > 0;
    }

    @Override
    public boolean check(String id) {
        return Utils.check(unitMapper.check(id));
    }

    @Override
    public boolean check(List<Unit> list) {
        return Utils.check(unitMapper.checkList(list));
    }

    @Override
    public void deleteBatch(List<Unit> list) {
        unitMapper.deleteBatch(list);
    }

    @Override
    public void enableBatch(List<Unit> list) {
        unitMapper.enableBatch(list);
    }

    @Override
    public void disableBatch(List<Unit> list) {
        unitMapper.disableBatch(list);
    }

    @Override
    public void insert(Unit unit) {
        unitMapper.insert(unit);
    }

    @Override
    public void remove(Unit unit) {
        unitMapper.deleteById(unit);
    }

    @Override
    public void update(Unit unit) {
        unitMapper.updateById(unit);
    }

    @Override
    public boolean checkSn(String id) {
        List<Boolean> list = unitMapper.checkSn(id);
        boolean flag = false;
        for (Boolean b : list) {
            if (b != null && b) {
                flag = true;
                break;
            }
        }
        return flag;
    }
}
